package sample.engine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;

import sample.dialogs.JavaTypeNameMatch;
import sample.exception.OpenJavaTypeByAuthorException;
/**
 *
 * @author CuiKun cuikunbj@cn.ibm.com
 *
 */
public class JavaTypeSearchEngine {

	private JavaTypeItemsFilter filter;
	private JavaTypeSearchRequestor requestor;

	public JavaTypeSearchEngine(JavaTypeSearchRequestor requestor) {
		this.requestor = requestor;
		filter = this.requestor.getPluginSearchFilter();
	}

	public void search() throws OpenJavaTypeByAuthorException {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IProject[] projects = workspace.getRoot().getProjects();
		for (IProject project : projects) {
			try {
				project.refreshLocal(0, null);
			} catch (CoreException e1) {
				throw new OpenJavaTypeByAuthorException(e1);
			}
			if (!project.isOpen()) {
				continue;
			}
			searchForJavaFile(project);
		}
	}

	/**
	 * edit author:yyu date:2011-4-21
	 */
	private void searchForJavaFile(IContainer parent)
			throws OpenJavaTypeByAuthorException {
		IResource[] members;
		try {
			members = parent.members();
		} catch (CoreException e) {
			throw new OpenJavaTypeByAuthorException(e);
		}
		for (IResource resource : members) {
			if (resource instanceof IContainer) {
				IContainer container = (IContainer) resource;
				searchForJavaFile(container);
			} else if (resource instanceof IFile) {
				IFile file = (IFile) resource;
				if ("jsp".equals(file.getFileExtension())) {
//					String location = getJspLocation(file);
					if (filter.matches(file)) {
						requestor.add(new JavaTypeNameMatch(file, getJspLocation(file)));
					}
				}
			}
		}
	}

	/**
	 * add author:yyu date:2011-4-21
	 */
	public String getJspLocation(IFile file){
		return file.getFullPath().toString();
	}

	public String getJavaTypeAuthorName(IFile file)
			throws OpenJavaTypeByAuthorException {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					file.getContents()));
			String mark = "@author";
			boolean inAnnotation = false;
			String line = reader.readLine();
			while (line != null) {
				if (!inAnnotation) {
					if (line.trim().contains("/**")) {
						inAnnotation = true;
					}
				} else {
					if (line.trim().contains("*/")) {
						inAnnotation = false;
					}
					if (line.trim().contains(mark)) {
						String name = line.substring(line.indexOf(mark)
								+ mark.length());
						name = name.trim();
						return name;
					}
				}
				if (line.trim().startsWith("public class")) {
					return null;
				}
				line = reader.readLine();
			}
		} catch (IOException e) {
			throw new OpenJavaTypeByAuthorException(e);
		} catch (CoreException e) {
			throw new OpenJavaTypeByAuthorException(e);
		}
		return null;
	}
}
